import { ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useCustomerApi } from '/@/api/customer';
import { useMemberLevelApi } from '/@/api/member';
import commonFunction from '/@/utils/commonFunction';
import { useTags } from '/@/stores/tags';

const storesTags = useTags();
storeToRefs(storesTags);
const { getPriceByField } = commonFunction();
const { getCustomerList, updateCustomer } = useCustomerApi();
const { getMemberLevelList } = useMemberLevelApi();

const page = () => {
	return {
		current: 1,
		pageSize: 10,
		total: 0,
	};
};

export const usrInit = () => {
	const state = reactive({
		editVisible: false,
		remlVisible: false,
		pid_loading: false,
		memberId: -1,
		totalLength: 0,
		totalMoney: '0',
		levelList: [],
		searchMemberList: [],
		tableData: {
			data: [],
			total: 0,
			loading: false,
			page: {
				pageSize: 7,
				current: 1,
				total: 0,
			},
		},
		formData: {
			username: '',
			levelTime: '',
		},
	});

	const mpage = ref(page());
	const getList = async (current) => {
		try {
			let params = {
				current: current,
				pageSize: mpage.value.pageSize,
				isVip: 1,
				levelTime: state.formData.levelTime,
				memberLevelId: state.formData.memberLevelId,
				username: state.formData.username,
			};
			console.log(params);
			let res = await getCustomerList(params);
			if (res.code !== 200) {
				return;
			}
			state.tableData.loading = false;

			mpage.value = {
				pageSize: res.data.pageSize,
				current: res.data.total,
				total: res.data.total,
			};

			let tempList = [];
			let data = res.data.data;
			tempList = data.map((item, index) => {
				return {
					...item,
					id: item.id,
					orderNumber: (res.data.current - 1) * 7 + index + 1,
					name: (item.firstname ? item.firstname : '') + ' ' + (item.lastname ? item.lastname : ''),
				};
			});
			console.log(tempList);
			state.tableData.data = tempList;
			// state.searchMemberList = deepClone(tempList);
			state.totalLength = res.data.orderCount;
			state.totalMoney =
				getPriceByField(res.data.usAmount, 'usAmount', 'Amount') +
				'&nbsp;&nbsp;' +
				getPriceByField(res.data.ukAmount, 'ukAmount', 'Amount') +
				'&nbsp;&nbsp;' +
				getPriceByField(res.data.euAmount, 'euAmount', 'Amount');
		} catch (error) {
			state.tableData.loading = false;
			ElMessage({
				type: 'error',
				message: '获取客户信息列表失败',
			});
		}
	};

	const remoteMethod = async (query) => {
		state.searchMemberList = [];
		try {
			if (query) {
				state.pid_loading = true;
				let params = {
					current: 1,
					isVip: 1,
					pageSize: 999999,
					username: query,
				};
				let res = await getCustomerList(params);
				if (res.code === 200) {
					state.pid_loading = false;
					let data = res.data.data;
					state.searchMemberList = data;
				}
			}
		} catch (error) {
			state.pid_loading = false;
			ElMessage({
				type: 'error',
				message: '获取客户信息列表失败',
			});
		}
	};
	const onUsernanSelectBlur = () => {
		state.searchMemberList = [];
	};

	const lPage = ref(page());
	const getLevelList = async () => {
		let res = await getMemberLevelList(lPage.value);
		if (res.code === 200) {
			state.levelList = res.data.data;
		}
	};

	const searchFun = () => {
		state.tableData.page = {
			pageSize: state.tableData.page.pageSize,
			current: 1,
			total: state.tableData.page.total,
		};
		getList(1);
	};

	const onOpenEdit = (item) => {
		state.editVisible = true;
		state.memberId = item.id;
	};

	const onCloseEdit = () => {
		state.editVisible = false;
	};

	const onOpenRem = (item) => {
		state.remlVisible = true;
		state.memberId = item.id;
	};

	return {
		state,
		mpage,
		getList,
		getLevelList,
		searchFun,
		onOpenEdit,
		onCloseEdit,
		onOpenRem,
		remoteMethod,
		onUsernanSelectBlur,
	};
};

export const useEdit = (option) => {
	const onSubmitMember = async (edirEl) => {
		console.log(edirEl.formData);
		try {
			edirEl.formData.isVip = 1;
			let res = await updateCustomer(edirEl.formData);
			if (res.code !== 200) {
				return;
			}
			option.onCloseEdit();
			option.getList(1);
			ElMessage({
				type: 'success',
				message: '保存成功！',
			});
		} catch (error) {
			ElMessage({
				type: 'error',
				message: '保存失败',
			});
		}
	};

	return {
		onSubmitMember,
	};
};
